<?php

namespace App\Exports;

use App\Models\DailyReportStatistics;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\FromQuery;

class DailyReportStatisticsExport extends BaseExport implements FromQuery
{
    public function headings(): array
    {
        return [
            '序号',
            '时间',
            '订单总数',
            '订单总金额',
            '充值总金额',
            '新增代理数',
        ];
    }

    public function map($row): array
    {
        return [
            $row['id'], // 序号
            $row['date'], // 时间
            $row['number_of_order'], // 订单总数
            number_format($row['total_amount_of_orders'] / 100, 2), // 订单总金额
            number_format($row['total_recharge_amount'] / 100, 2), // 充值总金额
            $row['total_number_of_new_agents'], // 新增代理数
        ];
    }

    public function query()
    {
        return DailyReportStatistics::query()
            ->when($this->request['date'] ?? '', function (Builder $builder, $date) {
                $builder->where('date', $date);
            });
    }
}
